"""
定义mysql数据库路由
"""
import random


# query_set >>>> db_router >>>> 链接数据库
# query_set >>>> using  >>>> 链接数据库
# query_set >>>> manger >>>> 链接数据苦
class MasterSlaveDBRouter(object):

    # 1、指定读操作的数据库配置
    def db_for_read(self, model, **hints):
        # model：模型类
        """
        Reads go to a randomly-chosen replica.

        """
        print("db_for_read1")
        return "slave"

    # 2、指定写操作的数据库配置
    def db_for_write(self, model, **hints):
        """
        Writes always go to primary.
        """
        print("Writes always go to primary1")
        return "master"

    def allow_relation(self, obj1, obj2, **hints):
        # 表示允许关联操作
        """
        Relations between objects are allowed if both objects are
        in the primary/replica pool.
        """
        print("allow_relation1")
        return True

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        All non-auth models end up in this pool.
        所有非身份验证模型最终都会在此池中。
        """
        print("allow_migrate")
        return True


class MasterSlaveDBRouter2(object):

    # 1、指定读操作的数据库配置
    def db_for_read(self, model, **hints):
        # model：模型类
        """
        Reads go to a randomly-chosen replica.

        """
        print("db_for_read2")
        return "slave"

    # 2、指定写操作的数据库配置
    def db_for_write(self, model, **hints):
        """
        Writes always go to primary.
        """
        print("Writes always go to primary2")
        return "master"

    def allow_relation(self, obj1, obj2, **hints):
        # 表示允许关联操作
        """
        Relations between objects are allowed if both objects are
        in the primary/replica pool.
        """
        print("allow_relation2")
        return True

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """
        All non-auth models end up in this pool.
        所有非身份验证模型最终都会在此池中。
        """
        print("allow_migrate2")
        return True
